package cn.engage.org.controller;

import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import cn.engage.org.domain.Department;
import cn.engage.org.query.DepartmentQuery;
import cn.engage.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    // 查询所有get请求localhost:8080/dept
    @GetMapping
    public List<Department> findAll(){
        return departmentService.findAll();
    }

   // 删除 delete请求 localhost:8080/dept/1
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    // 查询单个get请求 localhost:8080/dept/1
    @GetMapping("/{id}")
    public List<Department> findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    // 添加,修改 put请求
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if (null == department.getId()){
            // 添加
            departmentService.add(department);
        }else {
            // 修改
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    // 分页查询 post请求, currentPage,pageSize
    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return departmentService.queryByPage(query);
    }
    // 批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        departmentService.patchDelete(ids);

        return AjaxResult.me().success();
    }
    // 无限级联查询
    @PostMapping("/tree")
    public List<Department> DeptTree(){
        return departmentService.DeptTree();
    }
}
